package com.itheima.server;

import com.itheima.domain.Student;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

//服务器
@SuppressWarnings("all")
public class DemoServer {
    public static void main(String[] args) throws IOException {
        //程序一开始判断文件存不存在
        File f = new File("Day25codes\\user.txt");
        if (!f.exists()) {
            //如果文件不存在就创建
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
            //输出一个空集合;
            ArrayList<Student> list = new ArrayList<>();
            oos.writeObject(list);
            oos.close();
        }
        //启动服务器时读取学生id
        File f2 = new File("Day25codes\\user.txt");
        if (f2.exists()) {
            BufferedReader br = new BufferedReader(new FileReader(f2));
            String s = br.readLine();
            Student.stuid = Integer.parseInt(s);
            br.close();
        }
        //创建服务器对象;
        ServerSocket ss = new ServerSocket(9898);
        //循环可以接受多个客户端
        while (true) {
            //接收客户端;
            Socket s = ss.accept();
            //给客户端开启新线程
            new Thread(() -> {
                //一个客户的多次操作
                try {
                    while (true) {
                        //里面写多线程的代码
                        //接受客户端发来的数据
                        InputStream is = s.getInputStream();
                        byte[] arr = new byte[1024];
                        int len = is.read(arr);
                        String str = new String(arr, 0, len);
                        //切割字符串 第一段代表的是序号  后面代表的是参数
                        String[] strs = str.split(" ");
                        switch (strs[1]) {
                            case "[1]":
                                break;
                            case "[2]" :
                                break;
                            case "[3]":
                                break;
                            case "[4]":
                                break;
                            case "[5]":
                                break;

                        }
                    }
                } catch (Exception e) {

                }
            }).start();
        }
    }
}
